package br.ucb.tcc.persistencia;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import br.ucb.tcc.dto.PerfilDTO;

public class PerfilDAO {

	public static String INSERT_PERFIL = "INSERT INTO PERFIL VALUES(?,?,?,?,?,?,?,?,?,?,?,?)";
	public static String VERIFICA_PERFIL = "SELECT COUNT(*) FROM PERFIL WHERE PERFIL.IDPERFIL = ?";
	public static String UPDATE_PERFIL = "UPDATE PERFIL SET PERFIL.HASHTAGS = ? WHERE PERFIL.ID = ?";
	public static String INSERT_RELACOES = "INSERT INTO RELACOES VALUES (?,?)";

	public void inserirPerfil(PerfilDTO perfilDTO) {
		Connection connection = ConnectionFactory.getConnection();
		if (verificaRegistroDuplicado(perfilDTO.getId())) {
			try {
				PreparedStatement preparedStatement = connection
						.prepareStatement(UPDATE_PERFIL);
				preparedStatement.setLong(1, perfilDTO.getId());
				preparedStatement.setString(12, perfilDTO.getHashTags());
				preparedStatement.executeUpdate();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} else {
			PreparedStatement preparedStatement;
			try {
				preparedStatement = connection.prepareStatement(INSERT_PERFIL);
				preparedStatement.setLong(1, perfilDTO.getId());
				preparedStatement.setString(2, perfilDTO.getNomePerfil());
				preparedStatement.setString(3, perfilDTO.getNomeExibicao());
				preparedStatement.setString(4, perfilDTO.getDescricaoPerfil());
				preparedStatement.setString(5, perfilDTO.getLocalizacao());
				preparedStatement.setString(6, perfilDTO.getUrlImagem());
				preparedStatement.setInt(7, perfilDTO.getQtdSeguidores());
				preparedStatement.setInt(8, perfilDTO.getQtdAmigos());
				preparedStatement.setString(9, perfilDTO.getCorFundo());
				preparedStatement.setString(10, perfilDTO.getCorLink());
				preparedStatement.setString(11, perfilDTO.getUrlBackground());
				preparedStatement.setString(12, perfilDTO.getUltimoPost());
				preparedStatement.setString(13, perfilDTO.getHashTags());
				preparedStatement.executeUpdate();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

//	public void inserirTweets(PerfilDTO perfilDTO) {
//		for (Tweets tweets : perfilDTO.getTweets()) {
//			// TODO completar implementacao
//		}
//	}

	public boolean verificaRegistroDuplicado(Long perfil) {
		Connection connection = ConnectionFactory.getConnection();
		try {
			PreparedStatement preparedStatement = connection
					.prepareStatement(VERIFICA_PERFIL);
			preparedStatement.setLong(1, perfil);
			ResultSet executeQuery = preparedStatement.executeQuery();
			int contador = 0;
			while (executeQuery.next()) {
				contador++;
			}
			if (contador > 0) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	public void inserirRelacoes(Map<Long, Long> relacoes) {
		Connection connection = ConnectionFactory.getConnection();
		for (Long chaves : relacoes.keySet()) {
			PreparedStatement preparedStatement;
			try {
				preparedStatement = connection
						.prepareStatement(INSERT_RELACOES);
				preparedStatement.setLong(1, chaves);
				preparedStatement.setLong(2, relacoes.get(chaves));
				preparedStatement.execute();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
